#include "sessionsetting.h"
#include "ui_sessionsetting.h"
#include <QMessageBox>
#include <QPushButton>
#include <QDebug>
#include <QSerialPortInfo>
#include <QList>

SessionSetting::SessionSetting(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SessionSetting)
{
    ui->setupUi(this);
}

SessionSetting::~SessionSetting()
{
    delete ui;
}

void SessionSetting::init()
{
    this->setWindowTitle("SessionSetting");
    //设置tabWidget_SSH的tab标签的文本
    ui->tabWidget_SSH->setTabText(0,"Tab First");
    ui->tabWidget_SSH->setTabText(1,"Advance SSH settings");
    ui->tabWidget_SSH->setTabText(2,"Terminal settings");
    ui->tabWidget_SSH->setTabText(3,"Network settings");
    ui->tabWidget_SSH->setTabText(4,"Bookmark settings");

    //设置tabWidget_Serial的tab标签的文本
    ui->tabWidget_Serial->setTabText(0,"Tab First");
    ui->tabWidget_Serial->setTabText(1,"Advance Serial settings");
    ui->tabWidget_Serial->setTabText(2,"Terminal settings");
    ui->tabWidget_Serial->setTabText(3,"BookMark settings");

    //串口波特率的添加
    ui->stackedWidget->setCurrentIndex(0);
    QStringList strlist = {"1200","2400","4800","9600","19200","38400","57600","115200","460800","921600","230400"};
    for(int i = 0;i < strlist.count();i++)
    {
        ui->comboBox_serial_port_speed->addItem(strlist[i]);
    }
    ui->comboBox_serial_port_speed->setCurrentIndex(0);

    //电脑串口的获取
    QList<QSerialPortInfo> serialPortInfos = QSerialPortInfo::availablePorts();
    for(int i = 0;i < serialPortInfos.count();i++)
    {
        ui->comboBox_serial_port->addItem(serialPortInfos[i].portName() + "("+serialPortInfos[i].description()+")");
    }
}

void SessionSetting::on_btn_cancel_clicked()
{
    close();
}

void SessionSetting::on_btn_ok_clicked()
{
    switch(ui->stackedWidget->currentIndex())
    {
        case 1:
                ssh_enter();
                break;
        case 2:
                serial_enter();
                break;
        default:
                break;
    }
}

void SessionSetting::ssh_enter()
{
    QMessageBox::information(this,"提示","这是SSH");
    if((ui->lineEdit_host->text() == "") ||
       (ui->comboBox_username->currentText() == "") ||
       (ui->checkBox_username->isChecked() == 0) ||
       (ui->spinBox_port->text() == ""))
    {
        QMessageBox::critical(this,"警告","输入不能为空",QMessageBox::Ok | QMessageBox::Cancel);
        return;
    }
}

void SessionSetting::serial_enter()
{
    if((ui->comboBox_serial_port->currentText() == "") ||
       (ui->comboBox_serial_port_speed->currentText() == ""))
    {
        QMessageBox::critical(this,"警告","输入不能为空",QMessageBox::Ok | QMessageBox::Cancel);
        return;
    }

    QString portname;               //端口名
    int port_BaudRate;              //端口波特率
    portname= ui->comboBox_serial_port->currentText();
    port_BaudRate  = ui->comboBox_serial_port_speed->currentText().toInt();

    emit sendHostname_Port(portname,port_BaudRate);        //发送hostname和port
    close();
}

void SessionSetting::on_btn_SSH_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void SessionSetting::on_btn_Serial_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}
